package com.example.aspect;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author Sheeper
 * @date 2019/3/13.
 */
@Aspect
@Configuration
@Order(-1)
public class AccessAspect {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    @Pointcut("execution(public * com.example.controller..*.*(..))")
    public void access() {
    }

    @Around("access()")
    public Object doAround(ProceedingJoinPoint point) throws Throwable {
        long beginTime = System.currentTimeMillis();
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        Object[] objects = point.getArgs();
        String szPara="";
        if (objects.length == 1 && objects[0] instanceof Map) {
            Map<String, Object> params = (Map<String, Object>) objects[0];
            for(Map.Entry<String,Object> entry:params.entrySet())
            {
                szPara+=entry.getKey()+":"+entry.getValue()+";";
            }
        }

      //  logger.info(request.getMethod() + ":" + request.getRequestURI()+"__"+szPara);

        return point.proceed();
    }


}
